package cn.ffcs.uec.insurance.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.insurance.dao.UecBmDao;
import cn.ffcs.uec.insurance.model.UecBmclass;
import cn.ffcs.uec.insurance.model.UecClassitem;
import cn.ffcs.uec.insurance.model.UecPeriodstate;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecBmDao")
@Transactional
public class UecBmDaoImpl extends BaseDaoImpl implements UecBmDao {

	@Override
	public PageInfo queryPageInfoByUecBmclass(UecBmclass UecBmclass, int current,
			int pageSize) {
		if (null != UecBmclass) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_BMCLASS T WHERE T.STATUS_CD = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			if (null != UecBmclass.getName()) {
				sb.append(" AND T.NAME like ? ");
				params.add("%" + StringEscapeUtils.escapeSql(UecBmclass.getName()) + "%");
			}
			if (null != UecBmclass.getCode()) {
				sb.append(" AND T.CODE like ? ");
				params.add("%" + UecBmclass.getCode() + "%");
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecBmclass.class);
		}
		return null;
	}

	@Override
	public PageInfo queryPageInfoByUecClassitem(UecBmclass uecBmclass, int current, int pageSize) {
		if (null != uecBmclass) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_CLASSITEM T WHERE T.STATUS_CD = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			if (null != uecBmclass.getPkBmClass()) {
				sb.append(" AND T.PK_BM_CLASS = ? ");
				params.add(StringEscapeUtils.escapeSql(uecBmclass.getPkBmClass()));
			} else if(null != uecBmclass.getUecBmclassId()){
				sb.append(" AND T.BMCLASS_ID = ? ");
				params.add(uecBmclass.getUecBmclassId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecClassitem.class);
		}
		return null;
	}

	@Override
	public PageInfo queryPageInfoByUecPeriodstate(UecBmclass uecBmclass, int current, int pageSize) {
		if (null != uecBmclass) {
			StringBuilder sb = new StringBuilder(
					" SELECT T.*,T2.PERIOD uecPeriodName,decode(t.RECACUFLAG,'Y','是','N','否')  recacuflag  ,"
					+ "decode(t.CHECKFLAG,'Y','是','N','否')  checkflag,"
					+ "decode(t.ACCOUNTMARK,'Y','是','N','否')  accountmark, decode(t.ENABLEFLAG ,'Y','是','N','否') enableflag,"
					+ "decode(t.ISAPPROVED ,'Y','是','N','否')  isapproved"
					+ " FROM UEC_PERIODSTATE T "
					+ " LEFT JOIN UEC_PERIOD T2 ON (T.PK_BM_PERIOD = T2.PK_BM_PERIOD OR T.UEC_PERIOD_ID = T2.UEC_PERIOD_ID)"
					+ " WHERE T.STATUS_CD = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			if (null != uecBmclass.getPkBmClass()) {
				sb.append(" AND T.PK_BM_CLASS = ? ");
				params.add(StringEscapeUtils.escapeSql(uecBmclass.getPkBmClass()));
			} else if(null != uecBmclass.getUecBmclassId()){
				sb.append(" AND T.UEC_BMCLASS_ID = ? ");
				params.add(uecBmclass.getUecBmclassId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecPeriodstate.class);
		}
		return null;
	}

}
